<?php
require_once __DIR__ . "/Thrift/ClassLoader/ThriftClassLoader.php";

$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php';

// $loader->registerNamespace('Thrift', __DIR__ . '/../lib');
// $loader->registerDefinition('com', $GEN_DIR);
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__);
$loader->registerNamespace('Swoole', __DIR__);
$loader->registerNamespace('Services', __DIR__);
$loader->registerDefinition('Services',  __DIR__);
$loader->register();



use Swoole\Thrift\HttpStream;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\TMultiplexedProcessor;
use Services\com\penngo\LoginServiceHandler;
use Services\com\penngo\RegisterServiceHandler;
use Services\com\penngo\LoginServiceProcessor;
use Services\com\penngo\RegisterServiceProcessor;

// class RegisterServiceHandler implements \com\penngo\RegisterServiceIf {
//     public function createUser($name, $psw){
//         $user = new User();
//         $user->id = 2;
//         $user->name = $name.'_'. $_SERVER['HTTP_HOST'];
//         $user->password = $psw;
//         return $user;
//     }
// };
// class LoginServiceHandler implements \com\penngo\LoginServiceIf {
//     public function login($name, $psw){
//         $user = new User();
//         if($name == 'penngo' && $psw == '123'){
//             $user->id = 1;
//             $user->name = 'penngo' .'_'. $_SERVER['HTTP_HOST'];
//         }
//         return $user;
//     }
// };
// header('Content-Type', 'application/x-thrift');
// if (php_sapi_name() == 'cli') {
//     echo "\r\n";
// }


$serv = new swoole_http_server("192.168.17.103", 8080);

$serv->on('Request', function($request, $response) {
//     var_dump($request->get);
//     var_dump($request->post);
//     var_dump($request->cookie);
//     var_dump($request->files);
//     var_dump($request->header);
//     var_dump($request->server);
    echo "=============\n";
    $raw = $request->rawContent();
    var_dump($raw);
    echo "=============\n";
//     var_dump($request);
//     var_dump(is_resource($request));
//     $response->cookie("User", "Swoole");
//     $response->header("X-Server", "Swoole");
//     $response->end("<h1>Hello Swoole!</h1>");
    
    
//     $log = Logger::getLogger("=====");
    $transport = new TBufferedTransport(new HttpStream(TPhpStream::MODE_R | TPhpStream::MODE_W, $raw, $response));
    $protocol = new TBinaryProtocol($transport, true, true);
    $tMultiplexedProcessor = new TMultiplexedProcessor();
    
    $handler = new LoginServiceHandler();
    $loginServiceProcessor = new LoginServiceProcessor($handler);
    $tMultiplexedProcessor->registerProcessor("LoginService", $loginServiceProcessor);
    
    $registerService = new RegisterServiceHandler();
    $registerServiceProcessor = new RegisterServiceProcessor($registerService);
    $tMultiplexedProcessor->registerProcessor("RegisterService", $registerServiceProcessor);
    $transport->open();
    $tMultiplexedProcessor->process($protocol, $protocol);
    $transport->close();
//     $log->info("=====test");
    
});

    $serv->start();








?>